package com.demo.webapp.jcaptcha;

import java.awt.image.BufferedImage;
import java.io.Serializable;

import com.octo.captcha.image.ImageCaptcha;

/**
 * 可忽略大小写验证的验证对象<br/>
 * 创建该对象的目的是原有{@link com.octo.captcha.image.gimpy.Gimpy} 是一个计算大小写的.
 * IgnoreCaseGimpy 对象可以选择性的忽略验证字中的大小写.
 * 
 * @author ryuu.kk  at 2013-06-20
 */
public class IgnoreCaseGimpy extends ImageCaptcha implements Serializable {
	
	/**
	 * 版本序列化ID
	 */
	private static final long serialVersionUID = 3127491528938460530L;
	
	/**
	 * 用户输入的验证码
	 */
	private String response;

	/**
	 * 验证码验证忽略大小写标志
	 */
	private boolean ignore = true;
	
	protected IgnoreCaseGimpy(String question, BufferedImage challenge, String response) {
		super(question, challenge);
		this.response = response;
	}

	public final Boolean validateResponse(Object response) {
		return ((null != response) && (response instanceof String)) ? validateResponse((String) response)
				: Boolean.FALSE;
	}

	private final Boolean validateResponse(String response) {
		if (ignore) {
			// 忽略大小写验证
			return new Boolean(response.equalsIgnoreCase(this.response));
		} else {
			return new Boolean(response.equals(this.response));
		}
	}

	public boolean isIgnore() {
		return ignore;
	}

	public void setIgnore(boolean ignore) {
		this.ignore = ignore;
	}

	public String getResponse() {
		return response;
	}
}
